package com.yuhs.zixing.study.thread.juc.code002;

/**
 * @description synchronized 关键字 一个对象加锁
 * @author: yuhs
 * @date 2021/11/16 8:13 下午
 */
public class C01_Synchronized {

    private int count = 0;

    private Object object = new Object();

    public void m() {
        // 必须要拿到object锁才能执行里面的代码
        synchronized (object) {
            count++;
            System.out.println(Thread.currentThread().getName() + " count = " + count);
        }
    }

    public static void main(String[] args) {
        C01_Synchronized c01_synchronized = new C01_Synchronized();
        for (int i = 0; i < 20; i++) {
            new Thread(() -> c01_synchronized.m()).start();
        }
    }

}
